package com.qfedu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.BoatEntity;
import com.qfedu.entity.RouteEntity;
import com.qfedu.entity.RouteGroupEntity;
import com.qfedu.service.BoatService;
import com.qfedu.service.RouteService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.PostRemove;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class RouteController {

    @Reference(check = false)
    RouteService routeService;

    @Reference(check = false)
    BoatService boatService;

    @RequestMapping("jumpRoute")
    public String jumpRoute(HttpServletRequest request,RouteEntity re){


        PageInfo<RouteEntity> pageInfo = routeService.queryList(re);

        List<RouteGroupEntity> routeGroupEntities = routeService.queryGroupList();

        request.setAttribute("routeGroup",routeGroupEntities);

        request.setAttribute("pageInfo",pageInfo);

        request.setAttribute("routeen",re);

        return "transport";
    }


    @RequestMapping("/del")
    @ResponseBody
    public String del(int rid){

        boolean del = routeService.del(rid);
        if (del){
            return "0000";
        }
       return "1111";
    }



    @RequestMapping("/delAll")
    @ResponseBody
    public String  delAll(String ids){

        boolean b = routeService.deAll(ids);

        return "0000";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(RouteEntity re){

        RouteEntity routeEntity = routeService.queryRouteById(re.getRid());

        if (routeEntity.getRstate() == 1){
            routeEntity.setRstate(2);
        }else  {
            routeEntity.setRstate(1);
        }
        boolean update = routeService.update(routeEntity);


        if (routeEntity.getRstate() == 1){
            return "1";
        }else {
            return "2";
        }

    }

    @RequestMapping("jumpTang")
    public String jumpTang(HttpServletRequest request){

        List<RouteGroupEntity> rg = routeService.queryGroupList();

        List<BoatEntity> be = boatService.queryBoat();

        request.setAttribute("rg",rg);
        request.setAttribute("be",be);

        return "addRoute";
    }


    @RequestMapping("/addRoute")
    @ResponseBody
    public String addRoute(RouteEntity re){

        boolean insert = routeService.insert(re);


        return "0000";
    }

    @RequestMapping("/updateId")
    @ResponseBody
    public String updateId(RouteEntity re,HttpServletRequest request){
        request.getSession().setAttribute("rid",re);

        return "0000";
    }


    @RequestMapping("/jumpUpdate")
    public String jumpUpdate(HttpServletRequest request){

        RouteEntity rid = (RouteEntity) request.getSession().getAttribute("rid");
        RouteEntity routeEntity = routeService.queryRouteById(rid.getRid());
        request.getSession().removeAttribute("rid");

        List<RouteGroupEntity> routeGroupEntities = routeService.queryGroupList();

        List<BoatEntity> boatEntities = boatService.queryBoat();

        System.out.println(boatEntities);

        request.setAttribute("route",routeEntity);
        request.setAttribute("routegroup",routeGroupEntities);
        request.setAttribute("boat",boatEntities);
        return "insertRoute";
    }


    @RequestMapping("/updateRoute")
    @ResponseBody
    public String updateRoute(RouteEntity re){

        boolean update = routeService.update(re);

        return "0000";
    }


    //航线组操作
    @RequestMapping("/delgp")
    @ResponseBody
    public String delgp(int rgid){

        boolean delgp = routeService.delgp(rgid);

        if (delgp){
            return "0000";
        }
        return "1111";
    }



    @RequestMapping("/delAllboatgroup")
    @ResponseBody
    public String  delAllboatgroup(String ids){

        boolean b = routeService.delAllboatgroup(ids);

        return "0000";
    }


    @RequestMapping("routegroup")
    public String boatgroup(){

        return "addRouteGroup";
    }


    @RequestMapping("/addRouteGroup")
    @ResponseBody
    public String addRouteGroup(RouteGroupEntity rgp){
        boolean b = routeService.insertGroup(rgp);

        return "0000";
    }



    @RequestMapping("/updateRoutegp")
    @ResponseBody
    public String updateRoutegp(RouteGroupEntity rgp,HttpServletRequest request){

        request.getSession().setAttribute("rgp",rgp);

        return "0000";
    }


    @RequestMapping("/jumpUpdategp")
    public String jumpUpdategp(HttpServletRequest request){

        RouteGroupEntity rgp= (RouteGroupEntity) request.getSession().getAttribute("rgp");

        RouteGroupEntity routeGroupEntity = routeService.queryGroupById(rgp.getRgid());

        request.getSession().removeAttribute("rgp");

        request.setAttribute("routegp",routeGroupEntity);

        return "updateRoutegp";
    }



    @RequestMapping("/updateRgp")
    @ResponseBody
    public String updateRgp(RouteGroupEntity rgp){
        boolean b = routeService.updateGroup(rgp);

        return "0000";
    }
}
